#include <fcntl.h>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <unistd.h>
using namespace std;

class Human // 人 类 两个变量
{
private: // 虚函数,可攻击点
	virtual void give_shell()
	{
			setuid(geteuid());
			system("/bin/sh"); // 直接获取root shell
	}

protected:
	int age;
	string name;

public:
	virtual void introduce()
	{
			cout << "My name is " << name << endl;
			cout << "I am " << age << " years old" << endl;
	}
};

class Man : public Human // 男人类 继承人类 重写Introduce方法
{
public:
	Man(string name, int age)
	{
		this->name = name;
		this->age = age;
	}
	virtual void introduce()
	{
		Human::introduce();
		cout << "I am a nice guy!" << endl;
	}
};

class Woman : public Human // 女人类 继承人类 重写Introduce方法
{
public:
	Woman(string name, int age)
	{
		this->name = name;
		this->age = age;
	}
	virtual void introduce()
	{
		Human::introduce();
		cout << "I am a cute girl!" << endl;
	}
};

int main(int argc, char *argv[])
{
	Human *m = new Man("Jack", 25);
	Human *w = new Woman("Jill", 21);

	size_t len;
	char *data;
	unsigned int op;
	while (1)
	{
		cout << "1. use\n2. after\n3. free\n";
		cin >> op;

		switch (op)
		{
		case 1: //调用虚拟方法的虚函数introduce
				m->introduce();
				w->introduce();
				break;
		case 2: 
				len = atoi(argv[1]);
				data = new char[len]; // 申请内存
				read(open(argv[2], O_RDONLY), data, len); // 读入文件
				cout << "your data is allocated" << endl;
				break;
		case 3: // 释放指针
				delete m;
				delete w;
				break;
		default:
				break;
		}
	}
	return 0;
}

#if 0

sudo sysctl -w kernel.randomize_va_space=0
g++ -z execstack -o uaf-03 uaf-03.cpp
sudo chown root:root uaf-03
sudo chmod 4755 uaf-03

https://www.cnblogs.com/skprimin/p/16107988.html

#endif
